﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autocentro.Entities;
using System.Data.Objects;

namespace Autocentro.Respository
{
    public class ClienteRepository : MasterRepository, IClienteRepository
    {
        public List<CLIENTE> GetAllFromClientes()
        {
            var query = from c in context.Cliente
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<CLIENTE> GetFromClienteByCriterio(string criterio)
        {
            var query = from c in context.Cliente
                        select c;
            if (criterio!=null && criterio!=string.Empty && criterio!="")
            {
                query = from c in query
                        where c.Ruc_DNI.ToUpper().Contains(criterio.ToUpper()) ||
                        c.RazonSocial_Nombre.ToUpper().Contains(criterio.ToUpper()) ||
                        (c.RazonSocial_Nombre.ToUpper()+""+c.Ruc_DNI.ToUpper()).Contains(criterio.ToUpper())
                        select c;

            }
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public CLIENTE GetFromClienteByID(int id)
        {
            return context.Cliente.Find(id);
        }

        public bool ValidadDuplicidad(string ruc_dni, string nomRazonSocial, int id)
        {
            var query = from c in context.Cliente
                        where (c.Ruc_DNI.ToUpper().CompareTo(ruc_dni.ToUpper()) == 0 ||
                        c.RazonSocial_Nombre.ToUpper().CompareTo(nomRazonSocial.ToUpper()) == 0) &&
                        c.Id != id
                        select c;
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }

        public void AddCliente(CLIENTE cliente)
        {
            context.Cliente.Add(cliente);
            context.SaveChanges();
        }

        public void UpdateCliente(CLIENTE cliente)
        {
            context.Cliente.Attach(cliente);
            context.Entry(cliente).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveCliente(int id)
        {
            var existe = context.Cliente.Find(id);
            if (existe!=null)
            {
                context.Cliente.Remove(existe);
                context.SaveChanges();
            }
        }


        public CLIENTE GetFromClienteByRucDni(string rucDni)
        {
            var msg = "";
            try
            {
                var query = from c in context.Cliente
                        where c.Ruc_DNI.Equals(rucDni)
                        select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.FirstOrDefault();
            }
            catch (Exception)
            {
                throw new Exception(msg);
            }
        }
    }
}
